<%-- ExMobi JSP文件，注释和取消快捷键统一为Ctrl+/ 多行注释为Ctrl+Shift+/ --%>
<%@ page language="java" import="java.util.*,java.text.*"
 contentType="application/text; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/client/adapt.jsp"%>
<%@ include file="/client/adapt_extend.jsp"%>
<%@ include file="properties.jsp" %>
<%!
class MyThread extends Thread{
    private JSONObject param  = new JSONObject();
    private String lastTime="" ;
    private int count = 0;
    
    private boolean isstop = true;
    
    public void stopMe(boolean flag){
        this.isstop = false;
    }
    
    public MyThread(JSONObject param){
        this.param = param;
    }

    public void run() {
    	while(isstop){
    		this.count++;
    		this.param.put("tasktime", lastTime);
    		this.lastTime = send(this.getId(),this.param);
            try {
                this.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                this.isstop = false;
            }
    	}
    }
}
//轮询接口监听待办任务列表
public String send(long tid,JSONObject param){
	String time = "fail";
	try{
	  HttpUtil  httputil = new HttpUtil();
	  String url = param.getString("url")+ "/getTasksInfo";
	  HttpRequestBean httpRequestBean = new HttpRequestBean() ;
	  //设置url地址
	  httpRequestBean.setUrl(url);
	  //设置提交方式
	  httpRequestBean.setMethod("get");
	  //设置编码方式
	  httpRequestBean.setEnctype(1);
	  //设置自定义头
	  List<HttpHeader> headers = new ArrayList< HttpHeader >();
	  HttpHeader contentType = new HttpHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	  headers.add(contentType);
	  httpRequestBean.setHeaders(headers);
	  //设置请求参数
	  String paramstring = "account="+param.getString("account")+"&password="+param.getString("password")
	                     + "&groupid="+param.getString("groupid")+ "&userid="+param.getString("userid")
	                     + "&taskstatus="+param.getString("taskstatus")+ "&tasktime="+param.getString("tasktime");
	  httpRequestBean.setCustomHttpPostContent(paramstring);
	  HttpResponseBean  httpResponseBean = (HttpResponseBean)httputil.sendHttpRequest(httpRequestBean);//(HttpResponseBean)httputil.sendPost(url, "email=" username "&pwd=" password "&verifyCode=&save_login=1");
	  String body = httpResponseBean.getResponseBody("utf-8");
		
	  JSONObject jsonback = new JSONObject(body);
		if (new JSONObject(body).get("ret").equals("OK OK")){
			int length = jsonback.getJSONObject("result").getJSONArray("repairTasks").getJSONObject(0).getJSONArray("repairTask").length();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
			java.util.Date date=new java.util.Date();  
			time=sdf.format(date);

			System.out.println("threadid="+tid);
			System.out.println("recal list count="+ length+" time: "+time);
			//如果存在新列表，发起推送通知
			if(length>0){
			  //调用mobileark待办接口
			  
			  //调试时候用pnsdemo
			  
			}
			
			return time;
		}
	}catch(Exception e){
		System.out.println("errot ===="+ e);
		return time;
	}

}
%>
<%
  //启动线程
  Map<String,Object> params = aa.req.getParameters();
  JSONObject paramjson = new JSONObject(); 
  for(String key : params.keySet()){
	  paramjson.put(key, params.get(key));
  }
  //获取未派工状态的保障单
  paramjson.put("taskstatus", "1");
  
  String appid = "myfirst";
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/process/service/"+appid; 
  //System.out.println("url: "+basePath) ;
  paramjson.put("url", basePath);
  
  MyThread ht = new MyThread(paramjson);
  ht.start();
  JSONObject json = new JSONObject();
  json.put("ret", "ok");
  json.put("tid", ht.getId());
  out.println(json.toString());
%>
